#!/usr/bin/env python
# -*- coding: utf-8 -*- 
#methods to read the corpus from text files
#reads from both raw URL text and processed transcripts

import sys
import os
import corpus_reader
PATH = corpus_reader.__file__.split("/util/corpus_reader.py")[0]
if PATH == corpus_reader.__file__: #we are on windows
    PATH = corpus_reader.__file__.split("\\util\\corpus_reader.py")[0]
URL_CORPUS_PATH = PATH + "/" + "corpora" + "/" + "url_corpora"
sys.path.append(PATH + "\\objects")
sys.path.append(PATH + "/objects/models")
import utility as util
import phon_transcript
import phon_data
import phon_corpus
import crfsuite_model as crf
sys.path.append(util.PATH + "\\objects")
import re
import math
import numpy

#global values
#strings for each url corpus directory
JULIA = "Eng-NA_Goad_Julia"
SONYA = "Eng-NA_Goad_Sonya"
AMAHL = "Eng-UK_Smith_Amahl"
CLARA = "Romance_French_Clara"
THEO = "Romance_French_Theo"
#a list of all the corpus names
URL_CORPUS_NAMES = [JULIA, SONYA, AMAHL, CLARA, THEO]

def read_url_corpus(url_corpus_name = JULIA):
    """Takes a url based corpus name,
    and reads in all of its raw HTML text,
    processing it into a list of phon_transcripts"""
    transcripts = []
    for filename in os.listdir(URL_CORPUS_PATH + "/" + url_corpus_name):
        transcript = open(URL_CORPUS_PATH + "/" + url_corpus_name + "/" + filename, 'r')
        url_text = transcript.read()
        transcript.close()
        transcripts.append(util.url_text_to_phon_transcript(url_text))
    return transcripts

#gets the state transition weights
GET_STATE_WEIGHTS = re.compile(r'(?<=\(1\) )(?P<first>.) \-\-\> (?P<second>.): (?P<weight>[0-9]\.[0-9]+)')

GET_FEATURE_WEIGHTS = re.compile(r'(?<=\(0\) )(?P<first>.+) \-\-\> (?P<second>.+): (?P<weight>[0-9]\.[0-9]+)')
#groups of characters for making the FSAs

ALL = []

VOWELS = ['ɪ', 'e', 'i', 'ɛ', 'ɔ', 'ʊ', 'œ', 'ɑ', 'a', 'æ',
          'o', 'ɜ', 'ɵ', 'ʏ', 'u', 'ʉ', 'ɨ', 'ɒ', 'æ',
          'ʊ', 'ø', 'ɐ', 'ɶ', 'y', 'ɒ']


SCHWAS = ['ʌ', 'ə', 'ɚ']

VOWELS += SCHWAS

ALL += [VOWELS]

STOPS = ['b', 'ʔ', 'p', 'd', 't', 'ɡ', 'k', 'ɖ',
         'ɟ', 'ʦ', 'ʣ', 'ʧ', 'ʤ', 'c']

NASALS = ['m', 'n', 'ŋ', 'ɲ']

STOPS += NASALS

ALL += [STOPS]

FRICATIVES = ['ʃ', 'ʒ', 'ɕ', 's', 'h', 'x', 'f', 'ð', 'z', 'θ', 'ʝ',
              'v', 'χ', 'ç', 'β', 'ʀ', 'ɣ', 'ɸ', 'ħ', 'ʁ']

ALL += [FRICATIVES]

LIQUIDS = ['l', 'ɾ', 'ɹ', 'ɫ']

APPROXIMANTS = ['w', 'j', 'ʋ', 'ɥ', 'ʍ', 'ɻ', 'ʎ', 'ɭ']

LIQAPPROX = LIQUIDS #+ APPROXIMANTS

ALL += [LIQAPPROX]

NULL = ['-']

BOS = ['__BOS__']

EOS = ['__EOS__']

#ALL += [NULL]
FEAT = []
FEAT += [BOS]
FEAT += [EOS]

def fsa_weight(transitions, curr_symbols, next_symbols):
    CURRENT = 0
    NEXT = 1
    WEIGHT = 2
    total = 0
    for transition in transitions:
        if transition[CURRENT].encode('utf-8') in curr_symbols:
            if transition[NEXT].encode('utf-8') in next_symbols:
                total += math.exp(float(transition[WEIGHT]))
    return total

def print_all_weights(transitions, ALL = ALL):
    weights = numpy.zeros(len(ALL)) #matrix for each weight
    next_state_strs = []
    for group in range(0, len(ALL)):
        string = ""
        next_state_strs = []
        if group == 0: string += "Vowels --> "
        if group == 1: string += "Stops --> "
        if group == 2: string += "Fricatives --> "
        if group == 3: string += "Liq/Approx --> "
        if group == 4: string += "Null --> "
        for group2 in range(0, len(ALL)):
            total = fsa_weight(transitions, ALL[group], ALL[group2])
            weights[group2] = total
            string2 = string
            next_state_strs.append(string2)
            if group2 == 0: string2 += "Vowels"
            if group2 == 1: string2 += "Stops"
            if group2 == 2: string2 += "Fricatives"
            if group2 == 3: string2 += "Liq/Approx"
            if group2 == 4: string2 += "Null"
            #print string2 + ": " + str(total)
        for x in range(0, len(ALL)):
            print next_state_strs[x] + ": " + str(weights[x] / weights.sum())

def print_all_features(transitions, FEAT = FEAT):
    for group in range(0, len(ALL)):
        string = ""
        if group == 0: string += "Vowels --> "
        if group == 1: string += "Stops --> "
        if group == 2: string += "Fricatives --> "
        if group == 3: string += "Liq/Approx --> "
        if group == 4: string += "Null --> "
        for group2 in range(0, len(FEAT)):
            total = fsa_weight(transitions, FEAT[group2], ALL[group])
            string2 = string
            if group2 == 0: string2 += "__BOS__"
            if group2 == 1: string2 += "__EOS__"
            print string2 + ": " + str(total)



        



c = phon_corpus.phon_corpus(CLARA)

j = phon_corpus.phon_corpus(JULIA)

s = phon_corpus.phon_corpus(SONYA)

a = phon_corpus.phon_corpus(AMAHL)

t = phon_corpus.phon_corpus(THEO)
    
    
    
    
        
        
    
